//
//  GQHomeDetailViewCell.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/12.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQHomeDetailViewCell.h"
#define kHomeCellMargin 10

@interface GQHomeDetailViewCell ()

@property (weak, nonatomic) UIButton *iconImage;
@property (weak, nonatomic) UIImageView *vertifyImage;
@property (weak, nonatomic) UILabel *nameLabel;
@property (weak, nonatomic) UILabel *honorLabel;
@property (weak, nonatomic) UILabel *timeLabel;
@property (weak, nonatomic) UIImageView *imageShow;
@property (weak, nonatomic) UIView *containView;
@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) UILabel *channerLabel;
@property (weak, nonatomic) UIView *interactionView;
@property (weak, nonatomic) UIButton *moreBtn;
@property (weak, nonatomic) UIButton *commentBtn;
@property (weak, nonatomic) UIButton *praiseBtn;
@property (weak, nonatomic) UILabel *praiseCountLable;
@property (weak, nonatomic) UIView *praiseIcons;
@property (weak, nonatomic) UILabel *commentLabel;
@property (weak, nonatomic) UILabel *locationLabel;
@property (weak, nonatomic) UILabel *commonCountLable;
@property (weak, nonatomic) UIView *voteArea;
@property (weak, nonatomic) UIView *commonView;

@end

@implementation GQHomeDetailViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpChildView];
    }
    return self;
}

- (void)setUpChildView
{
    //创建头像
    UIButton *iconImage = [[UIButton alloc] init];
    [iconImage setImage:[UIImage imageWithRoundName:@"icon"] forState:UIControlStateNormal];
    self.iconImage = iconImage;
    [self addSubview:iconImage];
    
    //创建认证图标
    UIImageView *vertifyImage = [[UIImageView alloc] init];
    vertifyImage.image = [UIImage imageNamed:@"relationship_famous_accreditation"];
    self.vertifyImage = vertifyImage;
    [iconImage addSubview:vertifyImage];
    
    // 创建姓名标签
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:17];;
    nameLabel.text = @"张明林";
    self.nameLabel = nameLabel;
    [self addSubview:nameLabel];
    
    //创建头衔
    UILabel *honorLabel = [[UILabel alloc] init];
    honorLabel.font = [UIFont systemFontOfSize:14];
    honorLabel.textColor = [UIColor lightGrayColor];
    honorLabel.text = @"中国古董协会会员";
    self.honorLabel = honorLabel;
    [self addSubview:honorLabel];
    
    //创建timeLabel
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:12];
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.text = @"刚刚";
    self.timeLabel = timeLabel;
    [self addSubview:timeLabel];
    
    //发布地点
    UILabel *locationLabel = [[UILabel alloc] init];
    locationLabel.font = [UIFont systemFontOfSize:10];
    locationLabel.textColor = [UIColor lightGrayColor];
    locationLabel.textAlignment = NSTextAlignmentCenter;
    locationLabel.text = @"杭州大厦";
    self.locationLabel = locationLabel;
    [self addSubview:locationLabel];
    
    //创建图片展示
    UIImageView *imageShow = [[UIImageView alloc] init];
    imageShow.image = [UIImage imageWithHandleName:@"icon"];
    self.imageShow = imageShow;
    [self addSubview:imageShow];
    
    
    //创建一个图标的包含容器
    UIView *containView = [[UIView alloc] init];
    containView.backgroundColor = [UIColor lightGrayColor];
    self.containView = containView;
    
    //创建容器内的标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.text = @"昨天刚从古玩店里淘到的宝贝";
    self.titleLabel = titleLabel;
    [containView addSubview:titleLabel];
    
    //创建点赞头像
    NSInteger count = 5;
    CGFloat imageWidth = 25;
    
    UIView *praiseIcons = [[UIView alloc] init];
    self.praiseIcons = praiseIcons;
    
    for (int i = 0; i < count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithRoundName:@"icon"]];
        imageView.x = 10 + (imageWidth + 5) * i;
        imageView.y = 0;
        imageView.width = imageWidth;
        imageView.height = imageWidth;
        [praiseIcons addSubview:imageView];
    }
    
    [containView addSubview:praiseIcons];
    
    //创建点赞数的标签
    NSString *praiseStr = @"200个赞";
    UILabel *praiseCountLable = [[UILabel alloc] init];
    praiseCountLable.text = praiseStr;
    praiseCountLable.font = [UIFont systemFontOfSize:10];
    self.praiseCountLable = praiseCountLable;
    [containView addSubview:praiseCountLable];
    
    //创建评论标签
    NSString *commonstr = @"200个评论";
    UILabel *commonCountLable = [[UILabel alloc] init];
    commonCountLable.text = commonstr;
    commonCountLable.font = [UIFont systemFontOfSize:10];
    self.commonCountLable = commonCountLable;
    [containView addSubview:commonCountLable];
    
    //创建评论区
    NSInteger voteCount = 4;
    CGFloat voteCellHeight = 40;
    UIView *voteArea = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), self.width, 40 * voteCount + 5)];
    self.voteArea = voteArea;
    for (int i = 0; i < voteCount; i++) {
        UIView *voteCell = [[UIView alloc] init];
        voteCell.backgroundColor = [UIColor whiteColor];
        voteCell.x = 0;
        voteCell.y = 1 + (voteCellHeight + 1) * i;
        voteCell.height = voteCellHeight;
        voteCell.width = self.width;
        voteCell.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(voteCellDidClick)];
        [voteCell addGestureRecognizer:tap];
        
        UILabel *voteLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, voteCell.width, voteCell.height)];
        voteLabel.textAlignment = NSTextAlignmentCenter;
        voteLabel.text = @"唐代";
        voteLabel.font = [UIFont systemFontOfSize:12];
        voteLabel.textColor = [UIColor blackColor];
        [voteCell addSubview:voteLabel];
        
        UILabel *rateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - 70, 0, 60, voteCell.height)];
        rateLabel.text = @"30%(6)";
        rateLabel.font = [UIFont systemFontOfSize:12];
        rateLabel.textAlignment = NSTextAlignmentCenter;
        rateLabel.textColor = [UIColor blackColor];
        [voteCell addSubview:rateLabel];
        [voteArea addSubview:voteCell];
        
        UILabel *coverLabel = [[UILabel alloc] init];
        coverLabel.x = 0;
        coverLabel.y = 0;
        coverLabel.height = voteCell.height;
        coverLabel.width = self.width * 0.8;
        coverLabel.backgroundColor = [UIColor yellowColor];
        coverLabel.alpha = 0.5;
        [voteCell addSubview:coverLabel];
    }
    voteArea.backgroundColor = [UIColor orangeColor];
    [self.containView addSubview:voteArea];
    
    UIView *commonView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(voteArea.frame), self.width, 123)];
    commonView.backgroundColor = [UIColor lightGrayColor];
    
    for (int i = 0; i < 3; i++) {
        UILabel *commonLabel = [[UILabel alloc] init];
        commonLabel.backgroundColor = [UIColor whiteColor];
        commonLabel.x = 0;
        commonLabel.y = i * 41;
        commonLabel.width = self.width;
        commonLabel.height = 40;
        commonLabel.text = @"  杭州：你是谁";
        commonLabel.font = [UIFont systemFontOfSize:14];
        commonLabel.textColor = [UIColor blackColor];
        [commonView addSubview:commonLabel];
    }
    [self.containView addSubview:commonView];
    self.commonView = commonView;
    
    
    //创建交互按钮包含容器
    UIView *interactionView = [[UIView alloc] init];
    self.interactionView = interactionView;
    interactionView.backgroundColor = [UIColor whiteColor];
    
    //创建容器内的频道标签
    UILabel *channerLabel = [[UILabel alloc] init];
    channerLabel.font = [UIFont systemFontOfSize:12];
    channerLabel.text = @"#陶瓷紫砂#交易买卖";
    self.channerLabel = channerLabel;
    [interactionView addSubview:channerLabel];
    
    //创建获取更多的按钮
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setBackgroundImage:[UIImage imageNamed:@"home_more"] forState:UIControlStateNormal];
    moreBtn.layer.masksToBounds = YES;
    moreBtn.layer.cornerRadius = 10;
   // [moreBtn addTarget:self action:@selector(moreBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.moreBtn = moreBtn;
    [interactionView addSubview:moreBtn];
    
    //创建评论按钮
    UIButton *commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commentBtn.layer.masksToBounds = YES;
    commentBtn.layer.cornerRadius = 10;
    [commentBtn setBackgroundImage:[UIImage imageNamed:@"home_comment"] forState:UIControlStateNormal];
    //[commentBtn addTarget:self action:@selector(commentBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.commentBtn = commentBtn;
    [interactionView addSubview:commentBtn];
    
    //创建点赞的按钮
    
    UIButton *praiseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    praiseBtn.layer.masksToBounds = YES;
    praiseBtn.layer.cornerRadius = 10;
    [praiseBtn setBackgroundImage:[UIImage imageNamed:@"home_praise"] forState:UIControlStateNormal];
    [praiseBtn setBackgroundImage:[UIImage imageNamed:@"home_praised"] forState:UIControlStateSelected];
    [praiseBtn addTarget:self action:@selector(praiseBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.praiseBtn = praiseBtn;
    [interactionView addSubview:praiseBtn];
    
    [containView addSubview:interactionView];
    [self addSubview:containView];
    
}

//- (void)moreBtnDidClick:(UIButton *)btn
//{
//    if ([self.delegate respondsToSelector:@selector(GQHomeViewCellShareBtnDidClick)]) {
//        [self.delegate GQHomeViewCellShareBtnDidClick];
//    }
//}
//
//- (void)commentBtnDidClick:(UIButton *)btn
//{
//    if ([self.delegate respondsToSelector:@selector(GQHomeViewCellCommentBtnDidClick)]) {
//        [self.delegate GQHomeViewCellCommentBtnDidClick];
//    }
//}

- (void)praiseBtnDidClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat iconWidth = 45;
    CGFloat vertifyImageWidth = 15;
    CGFloat timeLabelWidth = 50;
    CGFloat moreBtnWidth = 22;
    CGFloat commenttBtnWidth = 22;
    CGFloat praiseWidth = 22;
    
    self.iconImage.frame = CGRectMake(kHomeCellMargin, kHomeCellMargin, iconWidth, iconWidth);
    self.vertifyImage.frame = CGRectMake(self.iconImage.width - 15, self.iconImage.width - 15, vertifyImageWidth, vertifyImageWidth);
    self.nameLabel.frame = CGRectMake(kHomeCellMargin + CGRectGetMaxY(self.iconImage.frame), kHomeCellMargin + 5, 100, 20);
    self.honorLabel.frame = CGRectMake(self.nameLabel.x, kHomeCellMargin + 5 + self.nameLabel.height, 150, 16);
    self.timeLabel.frame = CGRectMake(self.width - kHomeCellMargin * 2 - timeLabelWidth, kHomeCellMargin, timeLabelWidth, 20);
    self.locationLabel.frame = CGRectMake(self.timeLabel.x, CGRectGetMaxY(self.timeLabel.frame), self.timeLabel.width, self.timeLabel.height);
    self.imageShow.frame = CGRectMake(0, CGRectGetMaxY(self.iconImage.frame) + kHomeCellMargin, self.width, self.width);
    
    self.containView.frame = CGRectMake(0, CGRectGetMaxY(self.imageShow.frame), kScreenWidth, 100);
    self.titleLabel.frame = CGRectMake(kHomeCellMargin, kHomeCellMargin, kScreenWidth - 2 * kHomeCellMargin, 20);
    //    self.voteArea.width = self.width;
    
    
    
    self.praiseIcons.frame = CGRectMake(0, CGRectGetMaxY(self.voteArea.frame) + 5, self.width - 2 * kHomeCellMargin, 25);
    
    NSString *praiseStr = @"200个赞";
    CGSize praiseSize = [praiseStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) options:0 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    self.praiseCountLable.frame = CGRectMake(kHomeCellMargin, CGRectGetMaxY(self.praiseIcons.frame) + 5, praiseSize.width, praiseSize.height);
    
    NSString *commonStr = @"200个评论";
    CGSize commonSize = [commonStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) options:0 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    self.commonCountLable.frame = CGRectMake(CGRectGetMaxX(self.praiseCountLable.frame) + 3, CGRectGetMaxY(self.praiseIcons.frame) + 5, commonSize.width, commonSize.height);
    
    
    self.interactionView.frame = CGRectMake(0, CGRectGetMaxY(self.commonView.frame), self.width, 30);
    self.channerLabel.frame = CGRectMake(kHomeCellMargin, 0 + kHomeCellMargin / 2, 150, 20);
    
    
    //交互点赞等按钮
    self.moreBtn.x = self.interactionView.width - moreBtnWidth - 10;
    self.moreBtn.y = 4;
    self.moreBtn.width = moreBtnWidth;
    self.moreBtn.height = moreBtnWidth;
    
    self.commentBtn.x = self.interactionView.width - kHomeCellMargin - commenttBtnWidth - moreBtnWidth - 10;
    self.commentBtn.y = 4;
    self.commentBtn.width = commenttBtnWidth;
    self.commentBtn.height = commenttBtnWidth;
    
    self.praiseBtn.x = self.interactionView.width - moreBtnWidth - praiseWidth - commenttBtnWidth - 2 * kHomeCellMargin - 10;
    self.praiseBtn.y = 4;
    self.praiseBtn.width = praiseWidth;
    self.praiseBtn.height = praiseWidth;
    self.containView.height = CGRectGetMaxY(self.interactionView.frame) + 10;
}

- (void)voteCellDidClick
{
    NSLog(@"111");
}


@end
